﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using CONEK911Manager.DTO;

namespace CONEK911Manager.DAO
{
    class AccountDAO
    {
        readonly string _connectionString = ConfigurationManager.ConnectionStrings["Conek911ConnectionString"].ConnectionString;
        public bool AddAccount(AccountDTO account)
        {
            try
            {
                using (var con = new SqlConnection(_connectionString))
                {
                    const string sql = "INSERT into [Account] (Account, FullName, IDCard, Address,          BankAccount, BankName, BankAddress, ActiveID, IsAutoship, Ancessor, Phone, DistrictID, WardID) VALUES (@account, @fullname, @idCard, @address, @bankAccount, @bankName, @bankAddress, @activeId, @isAutoship, @ancessor, @phone, @districtId, @wardId)";
                    using (var sqlAddAccount = new SqlCommand(sql))
                    {
                        sqlAddAccount.Connection = con;
                        sqlAddAccount.Parameters.Add(new SqlParameter("@account", account.Account));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@fullname", account.FullName));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@idCard", account.IdCard));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@address", account.Address));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@bankAccount", account.BankAccount));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@bankName", account.BankName));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@bankAddress", account.BankAddress));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@activeId", account.ActiveId));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@isAutoship", account.IsAutoShip));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@ancessor", account.Ancessor));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@phone", account.Phone));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@districtId", account.DistrictId));
                        sqlAddAccount.Parameters.Add(new SqlParameter("@wardId", account.WardId));
                        con.Open();
                        var result = sqlAddAccount.ExecuteNonQuery();
                        con.Close();
                        if (result == 1)
                        {
                            return true;
                        }
                        return false;
                    }
                }
            } catch (Exception e)
            {
                try
                {
                    Logger.WriteLog(e.Message + "-" + e.StackTrace);
                }
                catch
                { }
                return false;
            }
        }

        public List<AccountDTO> GetAllAccount()
        {
            var listAccount = new List<AccountDTO>();
            using (var con = new SqlConnection(_connectionString))
            {
                const string sql = "SELECT * FROM [Account]";
                using (var cmd = new SqlCommand(sql))
                {
                    cmd.Connection = con;
                    con.Open();
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var account = new AccountDTO
                                              {
                                                  Account = Convert.ToString(reader["Account"]),
                                                  FullName = Convert.ToString(reader["Name"]),
                                                  IdCard = Convert.ToString(reader["IDCard"]),
                                                  Address = Convert.ToString(reader["Address"]),
                                                  BankAccount = Convert.ToString(reader["BankAccount"]),
                                                  BankName = Convert.ToString(reader["BankName"]),
                                                  BankAddress = Convert.ToString(reader["BankAddress"]),
                                                  ActiveId = int.Parse(Convert.ToString(reader["ActiveID"])),
                                                  IsAutoShip = bool.Parse(Convert.ToString(reader["IsAutoShip"])),
                                                  Ancessor = Convert.ToString(reader["Ancessor"]),
                                                  Phone = Convert.ToString(reader["Ancessor"]),
                                                  WardId = int.Parse(Convert.ToString(reader["WardID"])),
                                                  DistrictId = int.Parse(Convert.ToString(reader["DistrictID"])),
                                              };
                            listAccount.Add(account);
                        }
                    }
                    con.Close();
                }
            }

            return listAccount;
        }
    }
}
